<template>
  <div class="draggableItem">
    <component :is="componentTag" @onConfirm="onConfirm" v-show="element.isShow || status === 'edit'"></component>
    <div class="delIcon" v-if="elementId === activeId && status === 'edit'" @click="deleteItem">
      <div class="popper__arrow"></div>
      <van-icon name="delete-o" />
    </div>
  </div>
</template>

<script>
import { componentMap } from '../../data'
import { mapState, mapMutations, mapGetters } from 'vuex'

export default {
  name: 'draggableItem',
  props: {
    element: Object
  },
  data() {
    return {}
  },
  provide() {
    return {
      element: this.element
    }
  },
  computed: {
    ...mapState('pageData', ['activeFormData', 'status']),
    ...mapGetters('pageData', ['activeId']),
    elementId() {
      return this.element.formId
    },
    componentTag() {
      return componentMap[this.element.tag]
    }
  },
  methods: {
    ...mapMutations('pageData', ['DEL', 'SETVMODEL']),
    deleteItem() {
      // this.$emit('deleteItem', this.elementId)
      this.DEL(this.elementId)
    },
    onConfirm(val) {
      this.SETVMODEL({
        formId: this.elementId,
        value: val
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.draggableItem {
  position: relative;
  .delIcon {
    position: absolute;
    right: -60px;
    top: 50%;
    transform: translateY(-50%);
    padding: 2px 0;
    width: 50px;
    background: #fff;
    box-shadow: 0px 0px 10px rgb(0 0 0 / 10%);
    border: solid 1px #eee;
    text-align: center;
    cursor: pointer;
    z-index: 99;
    &:hover {
      color: red;
    }
  }

  .popper__arrow {
    content: '';
    position: absolute;
    right: 100%;
    top: 5px;
    width: 0;
    height: 0;
    border-width: 6px;
    border-style: solid;
    border-color: transparent;
    margin-bottom: 6px;
    border-right-width: 6px;
    border-right-color: currentColor;
    color: #eee;
    &:after {
      position: absolute;
      content: ' ';
      left: -5px;
      top: -6px;
      width: 0;
      height: 0;
      border-width: 6px;
      border-style: solid;
      border-color: transparent;
      margin-bottom: 6px;
      border-right-width: 6px;
      border-right-color: currentColor;
      color: #fff;
    }
  }
}

.active {
  ::v-deep .van-cell {
    background: #f6f7ff !important;
  }
}
::v-deep .van-cell {
  &:hover {
    background: #f6f7ff !important;
  }
}
</style>